package com.baidu.speech.qr_code;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.speech.qr_code.ui.Entity_class.Password;
import com.baidu.speech.qr_code.ui.Main.MainActivity_register;
import com.baidu.speech.qr_code.ui.MainActivity_image;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

    private EditText et_sign_in;
    private EditText et_passwoord;
    private Button bu_sign_in;
    private TextView tv_register;
    private TextView tv_forget_password;
    private List<Password> passwordList = new ArrayList<>();
    private CircleImageView iv_head_portrait;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        iv_head_portrait = (CircleImageView) findViewById(R.id.iv_head_portrait);
        et_sign_in = (EditText) findViewById(R.id.et_sign_in);
        et_passwoord = (EditText) findViewById(R.id.et_passwoord);
        bu_sign_in = (Button) findViewById(R.id.bu_sign_in);
        tv_register = (TextView) findViewById(R.id.tv_register);
//        tv_forget_password = (TextView) findViewById(R.id.tv_forget_password);
        bu_sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (et_sign_in.getText().toString().trim().equals("") || et_passwoord.getText().toString().trim().equals("")) {
                    Toast.makeText(MainActivity.this, "账号或密码为空", Toast.LENGTH_SHORT).show();
                } else if (forB()) {
                    startActivity(new Intent(MainActivity.this, MainActivity_image.class));
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, MainActivity_register.class));
            }
        });
//        tv_forget_password.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(MainActivity.this, "找回密码", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    private Boolean forB() {
        Boolean b = false;
        List<Password> passwords = LitePal.findAll(Password.class);
        for (int i = 0; i < passwords.size(); i++) {
            if (et_sign_in.getText().toString().trim().equals(passwords.get(i).getSign_in()) && et_passwoord.getText().toString().trim().equals(passwords.get(i).getPassword())) {
                b = true;
            }
        }
        return b;
    }
}